//
//  HomeRecommendCateView.swift
//  zhonglb_iOS
//
//  Created by Rowling on 2021/10/6.
//

import UIKit

class HomeRecommendCateView: UIView {
    
    public var itemClick : ((_ index : Int, _ item : HomeCate)->())?
    
    var dataSorce = [HomeCate]() {
        didSet {
            setUI()
        }
    }
    
    var column = 5
    
    var itemHeight : CGFloat = 60
    
    var inset = UIEdgeInsets(top: 0, left: 12, bottom: 16, right: 0)
    
    var itemWidth : CGFloat = 48
    
    var space : CGFloat {
        get {
            ((SCREEN_W - inset.left - inset.right) - itemWidth * CGFloat(column)) / CGFloat(column - 1)
        }
    }
    
    var lineSpace : CGFloat = 8
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    func setUI(){
        subviews.forEach({$0.removeFromSuperview()})
        var last : UIView?
        
        for i in 0 ..< (dataSorce.count > 10 ? 10 : dataSorce.count) {
            let data = dataSorce[i]
//            let width = SCREEN_W / CGFloat(column)
//            let height = rpx(102)
            let frame = CGRect(x: inset.left + CGFloat(i % column) * (itemWidth + space) , y: inset.top + CGFloat(i / column) * (itemHeight + lineSpace) , width: itemWidth, height: itemHeight)
            var v = UIView()
            v = itemView(imgStr: data.detailInfo().icon, title: data.detailInfo().title, frame: frame,tag : i)
            addSubview(v)
            last = v
        }
//        if let last = last {
//            snp.makeConstraints { (make) in
//                make.height.equalTo(last.zm.bottom)
//            }
//        }
    }
    
    func itemView(imgStr:String, title:String, frame : CGRect,tag : Int) -> UIView {
        let v = UIView(frame: frame)
        let imgW : CGFloat = 40
        let img = UIImageView(frame: CGRect(x: (frame.size.width - imgW) / 2, y: 0, width: imgW, height: imgW))
        img.contentMode = .scaleAspectFit
        img.layer.masksToBounds = true
        
        if imgStr.count > 0 {
            if imgStr.contains("http") {
                img.kf.setImage(with: URL(string: imgStr.zm.aliImg(h: img_low)), placeholder: UIImage(named: "def_img"))
            } else {
                img.image = UIImage.init(named: imgStr)
            }
        } else {
            img.image = UIImage(named: "def_img")
        }
        let label = ZMUtil.label(title: title, color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .medium))
        
        v.addSubview(img)
        v.addSubview(label)
        if imgStr == "home_0buy" {
            let topIcon = ZMUtil.imageView(imageName: "home_mfs")
            v.addSubview(topIcon)
            topIcon.snp.makeConstraints { make in
                make.bottom.equalTo(img.snp.top).offset(12)
                make.right.equalTo(img.snp.right).offset(7)
            }
        }
        label.snp.makeConstraints { (make) in
            make.top.equalTo(img.snp.bottom).offset(4)
            make.centerX.equalToSuperview()
//            make.width.equalToSuperview()
        }
        label.textAlignment = .center
        // 按钮
        let btn = UIButton()
        v.addSubview(btn)
        btn.style.snpEqualSuper()
        btn.tag = tag
        btn.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
        label.sizeToFit()
        return v
    }
    
    @objc func itemClick(_ sender : UIButton) {
        if let itemClick = itemClick {
            itemClick(sender.tag, dataSorce[sender.tag])
        }
    }
}

enum HomeCate : String {
    // 美团
    case mt
    // 饿了么
    case elm
    // 话费充值
    case phoneCharge
    // 任务
    case tasks
    // 0元购
    case zeroBuy
    // 淘宝省钱
    case taobao
    // 9.9
    case U99
    // 高佣
    case heighFee
    // 京东
    case JD
    // 拼多多
    case PDD
    // 电费
    case power
    // 加油
    case oil
    // 商学院
    case school
    
    case douyin
    
    case lianlian

    func detailInfo() -> (title : String, icon : String) {
        switch self {
        case .mt:
            return ("美团红包","home_mt")
        case .elm:
            return ("饿了么","home_elm")
        case .phoneCharge:
            return ("特惠话费","home_hf")
        case .tasks:
            return ("悬赏任务","home_task")
        case .zeroBuy:
            return ("新人免单","home_0buy")
        case .taobao:
            return ("淘宝省钱","home_taobao_v2")
        case .U99:
            return ("9.9包邮","home_99")
        case .heighFee:
            return ("高佣精选","home_gy")
        case .JD:
            return ("京东省钱","home_jd")
        case .PDD:
            return ("拼多多","home_pdd")
        case .power:
            return ("电费充值","home_df")
        case .oil:
            return ("特惠加油","home_oil")
        case .school:
            return ("商学院","home_oil")
        case .douyin:
            return ("抖音精选","home_douyin")
        case .lianlian:
            return ("联联周边游","home_lianlian")
        }
    }
}
